package com.thinkingstar.iads.uruleExample.action;


import com.bstek.urule.runtime.KnowledgeSession;
import com.thinkingstar.iads.common.jsonUtils.AjaxResult;
import com.thinkingstar.iads.test.urule.controller.RuleTool;
import com.thinkingstar.iads.uruleExample.entity.Customer;
import com.thinkingstar.iads.uruleExample.entity.LcdsbNew;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.Sql;
import org.nutz.dao.sql.SqlCallback;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.*;

/**
 * 计算 规则 测试
 * */
@At("/calculaterule")
@IocBean
public class CaculateTest {

    @Inject
    Dao dao;

    @At("/test")
    @Ok("json:full")
    public List<LcdsbNew> test(){

        Sql sql = Sqls.create("select * from ds_lcdsb where xmid='1' ");
        sql.setCallback(new SqlCallback() {
            @Override
            public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
                List<Map<String,Object>> list = new ArrayList<>();
                while (rs.next()){
                    Map<String,Object> map = new HashMap<>();
                    ResultSetMetaData metaData = rs.getMetaData();
                    int count = metaData.getColumnCount();
                    for (int i = count; i > 0; i--) {
                        String filed = metaData.getColumnName(i);
                        map.put(filed, rs.getString(filed));
                    }
                    list.add(map);
                }
                return list;
            }
        });
        dao.execute(sql);
        List<Map> list = sql.getList(Map.class);
        Map<String, Object> param = new HashMap<>();
        param.put("List",list);
        KnowledgeSession knowledgeSession = RuleTool.getSessionFactory("testExample/caculate_test");
        knowledgeSession.fireRules(param);

        Sql sql2 = Sqls.create("delete from ds_lcdsb_new where xmid='1' ");
        dao.execute(sql2);

        List<Map> listNew = (List<Map>) knowledgeSession.getParameter("ListNew");
        if (null != listNew &&listNew.size()>0) {
            listNew.get(0).put(".table","ds_lcdsb_new");
            dao.insert(listNew);
        }

        Sql sql3 = Sqls.create("SELECT * FROM ds_lcdsb_new  WHERE xmid='1'");
        sql.setCallback(Sqls.callback.entities());
        sql.setEntity(dao.getEntity(LcdsbNew.class));
        dao.execute(sql3);
        List<LcdsbNew> lists = sql.getList(LcdsbNew.class);

        return lists;

    }

    @At("/test2")
    @Ok("json:full")
    public AjaxResult test2(String project_id){
        Map<String, Object> param = new HashMap<>();
        param.put("project_id",project_id);
        KnowledgeSession knowledgeSession = RuleTool.getSessionFactory("列车对数表计算/cal_test");
        knowledgeSession.fireRules(param);
        return AjaxResult.success();
    }

    @At("/test3")
    @Ok("json:full")
    public AjaxResult test3(String project_id){
        Map<String, Object> param = new HashMap<>();
        param.put("project_id",project_id);
        KnowledgeSession knowledgeSession = RuleTool.getSessionFactory("列车对数表计算/cal_snsfzddczds");
        knowledgeSession.fireRules(param);
        return AjaxResult.success();
    }
}
